import { ref, toRefs } from 'vue';
import useDictStore from '@/store/modules/dict';
import { selectDict } from '@/api/common';

interface DictItem {
    label: string;
    value: string;
    listClass?: string;
    cssClass?: string;
}

interface DictResult {
    [key: string]: DictItem[];
}

/**
 * 获取字典数据
 */
export function useDict(...args: string[]) {
    const res = ref<DictResult>({});
    return (() => {
        args.forEach((dictType) => {
            res.value[dictType] = [];
            selectDict(dictType).then((resp) => {
                res.value[dictType] = resp.data.map((p: any) => ({
                    label: p.label,
                    value: p.value,
                    listClass: p.listClass,
                    cssClass: p.cssClass,
                }));
                // useDictStore().setDict(dictType, res.value[dictType]);
            });
        });
        return toRefs(res.value);
    })();
}